package com.kang.controller;

import com.kang.Vo.PhotoVo;
import com.kang.commons.Result;
import com.kang.bean.Photo;
import com.kang.service.PhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @program: photo_fawu
 * @description: 图片控制层
 * @author: K.faWu
 * @create: 2021-10-25 15:18
 **/
@RestController
@RequestMapping("/photo")
public class PhotoController {

    @Autowired
    private PhotoService photoService;

    /**
     * 首页展示照片
     */
    @GetMapping
    public Result<?> showPhoto(){
        PhotoVo photoVo = photoService.showPhoto();
        return Result.success(photoVo);
    }

    /**
     * 点赞
     */
    @PostMapping("/likes")
    public Result<?> likePhoto(@RequestBody Photo photo, HttpServletRequest request){
        Photo like = photoService.like(photo, request);
        return Result.success(like);
    }

    /**
     * 批量删除
     */
    @PostMapping("/deleteList")
    public Result<?> deleteList(@RequestBody List<Photo> photoList, HttpServletRequest request) throws ClassNotFoundException {
        photoService.deleteList(photoList,request);
        return Result.success();
    }

    /**
     * 删除图片
     */
    @DeleteMapping
    public Result<?> deleteById(Long id){
        photoService.deleteById(id);
        return Result.success();
    }

    /**
     * 获取指定相册的图片
     */
    @GetMapping("/byGroupId")
    public Result<?> queryByGroupId(Long groupId, HttpServletRequest request){
        PhotoVo photoVo = photoService.queryByGroupId(groupId,request);
        return Result.success(photoVo);
    }

    /**
     * 向相册中传照片
     */
    @PostMapping("/upload/{groupId}")
    public Result<?> upload(@RequestParam MultipartFile file, @PathVariable("groupId") Integer groupId){

        String path = photoService.uploadPhoto(file,groupId);
        return Result.success(path);
    }
}
